package com.cn.bulu.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cn.bulu.service.DealerService;
import com.cn.bulu.utils.BuluResult;
import com.cn.bulu.utils.redis.JedisPoolUtils;

import redis.clients.jedis.Jedis;

@Controller
@RequestMapping("/dealer")
public class DealerController {
	@Resource
	private DealerService dealerService;

	// 注册
	@RequestMapping("/register")
	@ResponseBody
	public BuluResult register(String phone, String password) {
		BuluResult buluResult = dealerService.register(phone, password);
		return buluResult;
	}

	// 看看手机号是否可以可以注册
	@RequestMapping("/isPhone")
	@ResponseBody
	public BuluResult isPhone(String phone) {
		BuluResult buluResult = dealerService.isPhone(phone);
		return buluResult;
	}

	// 完善信息
	@RequestMapping("/completeDealerData")
	@ResponseBody
	public BuluResult completeDealerData(Integer dealerId, String name, String referrer, String storeName,
			String detailAddress, String imgs) {
		BuluResult buluResult = dealerService.completeDealerData(dealerId, name, referrer, storeName, detailAddress,
				imgs);
		return buluResult;
	}

	// 登录
	@RequestMapping("/login")
	@ResponseBody
	public BuluResult login(String phone, String password, HttpServletRequest request) {
		BuluResult buluResult = dealerService.login(phone, password, request);
		return buluResult;
	}

	// 忘记密码(包括修改密码)
	@RequestMapping("/getDealerPassword")
	@ResponseBody
	public BuluResult getDealerPassword(String phone, String password) {
		BuluResult buluResult = dealerService.getDealerPassword(phone, password);
		return buluResult;
	}

	// 修改图片
	@RequestMapping("/updateImgs")
	@ResponseBody
	public BuluResult updateImgs(@RequestParam(value = "imgs[]") String[] imgs, HttpServletRequest request) {
		BuluResult buluResult = dealerService.updateImgs(imgs, request);
		return buluResult;
	}

	// 商品利润详情 Type是商品类型
	@RequestMapping("/selectProfits")
	@ResponseBody
	public BuluResult selectProfits(HttpServletRequest request, Integer type) {
		BuluResult buluResult = dealerService.selectProfits(request, type);
		return buluResult;
	}

	// 若为一级经销商查看自身下二级的商品利润
	@RequestMapping("/selectProfits2")
	@ResponseBody
	public BuluResult selectProfits2(HttpServletRequest request, Integer type) {
		BuluResult buluResult = dealerService.selectProfits2(request, type);
		return buluResult;
	}

	// 查询子账号
	@RequestMapping("/selectChild")
	@ResponseBody
	public BuluResult selectChild(HttpServletRequest request) {
		BuluResult buluResult = dealerService.selectChild(request);
		return buluResult;
	}

	// 停用子账号
	@RequestMapping("/chageChild")
	@ResponseBody
	public BuluResult chageChild(Integer customerId) {
		BuluResult buluResult = dealerService.chageChild(customerId);
		return buluResult;
	}

	// 修改子账号密码
	@RequestMapping("/chageChildPassword")
	@ResponseBody
	public BuluResult chageChildPassword(Integer customerId, String password) {
		BuluResult buluResult = dealerService.chageChildPassword(customerId, password);
		return buluResult;
	}

	// 申请子账号
	@RequestMapping("/applyChild")
	@ResponseBody
	public BuluResult applyChild(HttpServletRequest request, String nickName, String password) {
		BuluResult buluResult = dealerService.applyChild(request, nickName, password);
		return buluResult;
	}

	// 查询可提现金额
	@RequestMapping("/selectWallet")
	@ResponseBody
	public BuluResult selectWallet(HttpServletRequest request) {
		BuluResult buluResult = dealerService.selectWallet(request);
		return buluResult;
	}

	// 查询经销商推荐客户数
	@RequestMapping("/selectCustomerN")
	@ResponseBody
	public BuluResult selectCustomerN(HttpServletRequest request) {
		BuluResult buluResult = dealerService.selectCustomerN(request);
		return buluResult;
	}

	// 自营订单中的未完成
	@RequestMapping("/NoComplete")
	@ResponseBody
	public BuluResult NoComplete(HttpServletRequest request, Integer page) {
		BuluResult buluResult = dealerService.NoComplete(request, page);
		return buluResult;
	}

	// 自营中的已完成
	@RequestMapping("/YesComplete")
	@ResponseBody
	public BuluResult YesComplete(HttpServletRequest request) {
		BuluResult buluResult = dealerService.YesComplete(request);
		return buluResult;
	}

	// 查询二级经销商的未完成的订单
	@RequestMapping("/NoComplete2")
	@ResponseBody
	public BuluResult NoComplete2(HttpServletRequest request, Integer page) {
		BuluResult buluResult = dealerService.NoComplete2(request, page);
		return buluResult;
	}

	// 查询二级经销商的已完成订单
	@RequestMapping("/YesComplete2")
	@ResponseBody
	public BuluResult YesComplete2(HttpServletRequest request) {
		BuluResult buluResult = dealerService.YesComplete2(request);
		return buluResult;
	}

	// 点击提现记录
	@RequestMapping("/selectRecord")
	@ResponseBody
	public BuluResult selectRecord(HttpServletRequest request) {
		BuluResult buluResult = dealerService.selectRecord(request);
		return buluResult;
	}

	// 经销商自主下单测量订单
	@RequestMapping("/insertCIndent")
	@ResponseBody
	public BuluResult insertCIndent(HttpServletRequest request, String name, String phone, Date subscribeTime,
			Integer roomNumB, String city, String address, String remark) {
		BuluResult buluResult = dealerService.insertCIndent(request, name, phone, subscribeTime, roomNumB, city,
				address, remark);
		return buluResult;
	}

	// 经销商自主下单安装订单anyGoods="[{},{},]"大括号里面是商品
	@RequestMapping("/insertAIndent")
	@ResponseBody
	public BuluResult insertAIndent(HttpServletRequest request, String name, String phone, Date subscribeTime,
			String baseAddress,String detailAddress, String city, String address, String anyGoods) {
		BuluResult buluResult = dealerService.insertAIndent(request, name, phone, subscribeTime, baseAddress,detailAddress, city,
				address, anyGoods);
		return buluResult;
	}

	// 盈利详情
	@RequestMapping("/selectObtain")
	@ResponseBody
	public BuluResult selectObtain(HttpServletRequest request, Integer year) {
		BuluResult buluResult = dealerService.selectObtain(request, year);
		return buluResult;
	}

	// 点击盈利详情中年利润(自营)
	@RequestMapping("/selectProprietary")
	@ResponseBody
	public BuluResult selectProprietary(HttpServletRequest request, Integer year, Integer month) {
		BuluResult buluResult = dealerService.selectProprietary(request, year, month);
		return buluResult;
	}

	// 点击盈利详情中年利润(二级经销商)
	@RequestMapping("/selectScendProprietary")
	@ResponseBody
	public BuluResult selectScendProprietary(HttpServletRequest request, Integer year, Integer month) {
		BuluResult buluResult = dealerService.selectScendProprietary(request, year, month);
		return buluResult;
	}

	// 添加银行卡和提现密码和身份证图片
	@RequestMapping("/updateXimka")
	@ResponseBody
	public BuluResult updateXinka(HttpServletRequest request, String boundBank, String bankNumber,
			String recordPassword, String imgs) {
		BuluResult buluResult = dealerService.updateXinka(request, boundBank, bankNumber, recordPassword, imgs);
		return buluResult;
	}

	// 根据订单号查询其中商品
	@RequestMapping("/getGoodsByOrderId")
	@ResponseBody
	public BuluResult getGoodsByOrdersId(HttpServletRequest request, Integer orderId) {
		BuluResult buluResult = dealerService.getGoodsByOrderId(request, orderId);
		return buluResult;
	}

	// 点击盈利详情中年利润(自营)点击子账号
	@RequestMapping("/selectChildOrder")
	@ResponseBody
	public BuluResult selectChildOrder(HttpServletRequest request, Integer customerId, Integer year, Integer month) {
		BuluResult buluResult = dealerService.selectChildOrder(request, customerId, year, month);
		return buluResult;
	}

	// 根据订单号查询其中商品一级对二级的利润
	@RequestMapping("/selectScendGoods")
	@ResponseBody
	public BuluResult selectScendGoods(HttpServletRequest request, Integer orderId) {
		BuluResult buluResult = dealerService.selectScendGoods(request, orderId);
		return buluResult;
	}

	// 根据经销商号查询订单(已完成)
	@RequestMapping("/selectScendDealer")
	@ResponseBody
	public BuluResult selectScendDealer(HttpServletRequest request, Integer scendDealerId, Integer year,
			Integer month) {
		BuluResult buluResult = dealerService.selectScendDealer(request, scendDealerId, year, month);
		return buluResult;
	}

	// 查询个人信息
	@RequestMapping("/selectDealer")
	@ResponseBody
	public BuluResult selectDealer(HttpServletRequest request) {
		BuluResult buluResult = dealerService.selectDealer(request);
		return buluResult;
	}

	// 根据商品号查询商品的一些信息
	@RequestMapping("/selectXGoods")
	@ResponseBody
	public BuluResult selectXGoods(HttpServletRequest request, Integer goodsId) {
		BuluResult buluResult = dealerService.selectXGoods(request, goodsId);
		return buluResult;
	}

	// 退出登录
	@RequestMapping("/Logout")
	@ResponseBody
	public BuluResult Logout(HttpServletRequest request) {
		Jedis redis = JedisPoolUtils.getJedis();// 连接redis
		String sign = request.getParameter("sign");
		redis.del(sign);
		JedisPoolUtils.close(redis);
		request.getSession().removeAttribute("dealerId");
		request.getSession().invalidate();
		BuluResult buluResult = new BuluResult();
		buluResult.setStatus(0);
		return buluResult;
	}

	// 查询广告
	@RequestMapping("/selectGuanggao")
	@ResponseBody
	public BuluResult selectGuanggao() {
		BuluResult buluResult = dealerService.selectGuanggao();
		return buluResult;
	}
}
